package com.dlm.controller;

import com.dlm.dao.Evaluate;
import com.dlm.dao.Order;
import com.dlm.service.IEvaluateService;
import com.dlm.service.IOrderService;
import com.dlm.service.impl.EvaluateServiceImpl;
import com.dlm.service.impl.OrderServiceImpl;
import com.dlm.util.TypeUtil;
import com.google.common.base.Preconditions;
import com.jfinal.plugin.activerecord.Page;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * Created by chongsong on 15/2/1.
 * <mailto>chong.song@autonavi.com</mailto>
 */
public class EvaluateController extends BaseController {
    private Logger logger = Logger.getLogger(EvaluateController.class);
    private IEvaluateService service = new EvaluateServiceImpl();

    public void add() {
        long techId = getParaToLong("techId", 0l);
        String userId = getPara("userId");
        int pd = getParaToInt("pd", 0);
        int cd = getParaToInt("cd", 0);
        int td = getParaToInt("td", 0);
        int ada = getParaToInt("ada", 0);
        int adb = getParaToInt("adb", 0);
        int adc = getParaToInt("adc", 0);
        try {
            Preconditions.checkArgument((techId > 0), "techId: 不能为空,且必须为整数");
            Preconditions.checkArgument(StringUtils.isNotBlank(userId), "userId: 不能为空!");
            Preconditions.checkArgument((pd > 0), "pd: 不能为空,且必须为0-5");
            Preconditions.checkArgument((cd > 0), "cd: 不能为空,且必须为0-5");
            Preconditions.checkArgument((td > 0), "td: 不能为空,且必须为0-5");
            Preconditions.checkArgument((ada == 0 || ada == 1), "ada: 必须为0或者1.");
            Preconditions.checkArgument((adb == 0 || adb == 1), "adb: 必须为0或者1.");
            Preconditions.checkArgument((adc == 0 || adc == 1), "adc: 必须为0或者1.");
        } catch (Exception e) {
            renderJson(makeDto(1, e.getMessage(), null));
            return;
        }
        Evaluate evaluate = new Evaluate();
        evaluate.set(Evaluate.techId, techId);
        evaluate.set(Evaluate.userId, userId);
        evaluate.set(Evaluate.pd, pd);
        evaluate.set(Evaluate.cd, cd);
        evaluate.set(Evaluate.td, td);
        evaluate.set(Evaluate.ada, ada);
        evaluate.set(Evaluate.adb, adb);
        evaluate.set(Evaluate.adc, adc);
        boolean isSave = false;
        try {
            isSave = service.add(evaluate);
            if (isSave) {
                renderJson(makeDto(0, "ok", null));
                return;
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        renderJson(makeDto(1, "保存失败!", null));
    }

    public void del() {
        long id = getParaToLong("id", 0l);
        try {
            Preconditions.checkArgument(id > 0, "id不能为空且必须大于0!");
        } catch (Exception e) {
            renderJson(makeDto(1, e.getMessage(), null));
            return;
        }

        boolean isDel = false;
        try {
            isDel = service.delete(id);
            if (isDel) {
                renderJson(makeDto(0, "ok", null));
                return;
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        renderJson(makeDto(1, "删除失败", null));
    }

    public void find() {
        int pno = getParaToInt("pno", 1);
        int psize = getParaToInt("psize", 10);
        try {
            Page<Evaluate> page = service.findByPage(pno, psize);
            renderJson(makeDtoPage(0, "ok", page));
            return;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        renderJson(makeDto(1, "查询失败", null));
    }

    public void findDeg() {
        int pno = getParaToInt("pno", 1);
        int psize = getParaToInt("psize", 10);
        long techId = getParaToLong("techId", 0l);
        try {
            Preconditions.checkArgument((techId > 0), "techId不能为空,且必须为整数");
        } catch (Exception e) {
            renderJson(makeDto(1, e.getMessage(), null));
            return;
        }
        try {
            Evaluate evaluate = service.findDeg(techId);
            renderJson(makeDto(0, "成功", evaluate));
            return;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        renderJson(makeDto(1, "查询失败", null));
    }


}
